﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DataType;
using RemoteConnector;
using System.Data.SqlClient;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;

using System.Net;
using System.Xml;

using Utilities;
using System.Data.OleDb;

using Google.Documents;
using Google.Contacts; 

namespace ZohoConnector
{
    public class ZohoAuthorizationException : Exception
    {

    }

    public class ZohoConnector: IRemoteConnector
    {
        private static string ZOHO_API_KEY = "xynuQcN0KQl8lwyjpP0KPFRpKDMx6XStkQaMGWeIdlI$";
        private static string ZOHO_SERVICE = "ZohoSheet";
        private static string ZOHO_API_URL = "https://accounts.zoho.com/";
        private static string ZOHO_CRM_URL = "http://crm.zoho.com/";

        private string m_Ticket { get; set; }

        #region IRemoteConnector Members

        public void Login(object sender, Credentials cred)
        {
            string loginRequestString = ZOHO_API_URL +
                            "login?servicename=" +
                            ZOHO_SERVICE + "&" +
                            "FROM_AGENT=true" + "&" +
                            "LOGIN_ID=" + cred.m_Cred.Username + "&" +
                            "PASSWORD=" + cred.m_password;

            HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create(loginRequestString);
            loginRequest.Credentials = CredentialCache.DefaultCredentials;
            loginRequest.Method = "GET";

            HttpWebResponse loginResponse = (HttpWebResponse)loginRequest.GetResponse();

            Stream receiveStream = loginResponse.GetResponseStream();

            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

            string responseContent = readStream.ReadToEnd();
            loginResponse.Close();
            readStream.Close();

            //Analyze Response. 
            char[] seps = { '\n', '\r' };
            string[] responseValues = responseContent.Split(seps);

            if (GetLoginResult(responseValues) != true)
                throw new ZohoAuthorizationException();

            m_Ticket = GetLoginTicket(responseValues);

            Console.Write(responseContent);

        }

        public void LogOut()
        {
            throw new NotImplementedException();
        }

        public void GetContacts(object sender, FeedWrapper<Google.Contacts.Contact> feed)
        {
            throw new NotImplementedException();
        }

        public void GetDocuments(object sender, FeedWrapper<Google.Documents.Document> feed)
        {

            throw new NotImplementedException();
        }

        public void UploadDocument(object sender, DocumentInfo doc)
        {
            throw new NotImplementedException();
        }

        public void UpdateDocument(object sender, DocumentInfo doc)
        {
            throw new NotImplementedException();
        }

        public void GetFolders(object sender, FeedWrapper<Google.Documents.Document> feed)
        {
            throw new NotImplementedException();
        }

        public void DownloadDocument(object sender, DownloadInfo info)
        {
            throw new NotImplementedException();
        }

        public void ShareDocument(object sender, ShareInfoCollection info)
        {
            throw new NotImplementedException();
        }

        public void GetDocumentAcl(object sender, ShareInfoCollection info)
        {
            throw new NotImplementedException();
        }

        public void OpenDocumentInCloud(object sender, DocumentInfo doc)
        {
            throw new NotImplementedException();
        }

        public void LookForDocument(object sender, DocumentInfo info)
        {
            throw new NotImplementedException();
        }

        #endregion


        private bool GetLoginResult(string[] responseValues)
        {
            foreach (string param in responseValues)
            {
                if (param.StartsWith("RESULT="))
                {
                    string value = param.Substring(7);
                    if (value.Equals("TRUE"))
                        return true;
                }
            }

            return false;
        }

        private string GetLoginTicket(string[] responseValues)
        {
            string ticket = "";
            foreach (string param in responseValues)
            {
                if (param.StartsWith("TICKET="))
                {
                    ticket = param.Substring(7);
                    return ticket;
                }
            }
            return ticket;
        }

    }
}
